package xyf.p11.other.bit;

/**
 * ClassName: M0501_Insert
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:14 AM
 * Version: 1.0
 **/
public class M0501_Insert
{
    public int insertBits(int N, int M, int i, int j)
    {
        int[] nbits = new int[32];
        int[] mbits = new int[32];
        int nbitsNum = 0;
        int mbitsNum = 0;
        while (N != 0)
        {
            nbits[nbitsNum++] = N % 2;
            N /= 2;
        }
        while (M != 0)
        {
            mbits[mbitsNum++] = M % 2;
            M /= 2;
        }
        for (int k = i; k <= j; ++k)
        {
            nbits[k] = mbits[k - i];
        }
        int mask = 1;
        int ret = 0;
        for (int k = 0; k < 32; ++k)
        {
            ret += (nbits[k] * mask);
            mask <<= 1;
        }
        return ret;
    }
}